package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.itcast.constant.MessageConstant;
import com.itcast.constant.RedisMessageConstant;
import com.itcast.entity.Result;
import com.itcast.pojo.Member;
import com.itcast.service.OrderService;
import com.itcast.untils.SMSUtils;
import com.itcast.untils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 预约成功信息展示
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Map map;
        try {
            map = orderService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
    }

    /**
     * 手机发送登录验证码
     * @param telephone
     * @return
     */
    @RequestMapping("/send4Login")
    public Result send4Login(@RequestParam String telephone){
        //生成短信验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        System.out.println(code);
        //调用工具类发送短信
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            //短信发送成功将验证码存储在jedis中
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,5*60,code.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.LOGIN_FALL);
        }
        return new Result(true,MessageConstant.LOGIN_SUCCESS);
    }

    /**
     * 登录
     * @param map
     * @return
     */
    @RequestMapping("/check")
    public Result check(@RequestBody Map map, HttpServletResponse response){
        String  telephone = (String)map.get("telephone");
        //从jedis中获取验证码
        String s1 = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        String validateCode = (String)map.get("validateCode");
        if (s1 == null || validateCode == null || !s1.equals(validateCode)){
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }else{
            //判断是否是会员
           Member member = orderService.find(telephone);
           if (member == null){
               //当前用户不是会员，自动完成注册                 
               member = new Member();
               member.setPhoneNumber(telephone);
               member.setRegTime(new Date());
               orderService.add(member);
           }
            //登陆成功写入成功跟踪用户
            Cookie cookie = new Cookie("login_member_telephone",telephone);
           cookie.setPath("/");
           cookie.setMaxAge(60*60*24*30);
            response.addCookie(cookie);
            //保存会员信息到jedis中
            String json = JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone,60*30,json);
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        }
    }
}

